<?php
	header('Content-Type: text/html;charset=UTF-8');
?>

<?php
include('../../Redirect_modules.php');
if(clean_param($_REQUEST['values'],PARAM_NOTAGS) && ($_POST['values'] || $_REQUEST['ajax']))
{
	foreach($_REQUEST['values'] as $id=>$columns)
	{
                                $go=false;
                                $error=false;
		if($id!='new')
		{
                     $select_enroll=DBGet(DBQuery('SELECT TYPE FROM student_enrollment_codes WHERE ID=\''.$id.'\''));
            
			$sql = 'UPDATE student_enrollment_codes SET ';
							
			foreach($columns as $column=>$value)
			{
                                                            if(($select_enroll[1][TYPE]=='Roll' || $select_enroll[1][TYPE]=='TrnD' || $select_enroll[1][TYPE]=='TrnE' || $value=='Roll' ||  $value=='TrnD' || $value=='TrnE') && $column=='TYPE'){
                                                                $error=true;
                                                                continue;
                                                            }
                                                                        $value= paramlib_validation($column,trim($value));
				$sql .= $column.'=\''.str_replace("\'","''",$value).'\',';
                                                                        $go=true;
                                                            
			}
			$sql = substr($sql,0,-1) . ' WHERE ID=\''.$id.'\'';
			if($go)
                                                        DBQuery($sql);
//		}
                     if($error){
                         ShowErrPhp('Can\'t edit "Type" because it is not editable');
                    }
		}
		else
		{
                        if($columns[TYPE]!='Roll' &&  $columns[TYPE]!='TrnD' && $columns[TYPE]!='TrnE'){
			$sql = 'INSERT INTO student_enrollment_codes ';

			$fields = 'SYEAR,';
			$values = '\''.UserSyear().'\',';
                        
			$go = 0;
			foreach($columns as $column=>$value)
			{
				if(trim($value))
				{
                                        $value= paramlib_validation($column,$value);
					$fields .= $column.',';
					$values .= '\''.str_replace("\'","''",$value).'\',';
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
			
			if($go)
				DBQuery($sql);
		}
                else {
                    ShowErrPhp('You can\'t add any enrollment code in this type');
	}
}
	}
}

DrawBC("Students > ".ProgramTitle());

if(clean_param($_REQUEST['modfunc'],PARAM_ALPHAMOD)=='remove')
{
        $select_enroll=DBGet(DBQuery('SELECT TYPE FROM student_enrollment_codes WHERE ID=\''.$_REQUEST[id].'\''));
        
        if($select_enroll[1][TYPE]!='Roll' &&  $select_enroll[1][TYPE]!='TrnD' && $select_enroll[1][TYPE]!='TrnE'){
        $has_assigned_RET=DBGet(DBQuery('SELECT COUNT(*) AS TOTAL_ASSIGNED FROM student_enrollment WHERE  ENROLLMENT_CODE=\''.$_REQUEST[id].'\''));
	$has_assigned=$has_assigned_RET[1]['TOTAL_ASSIGNED'];
	if($has_assigned>0){
	UnableDeletePrompt('Cannot delete because enrollment codes are associated.');
	}else{
	if(DeletePrompt('enrollment code'))
	{
		DBQuery('DELETE FROM student_enrollment_codes WHERE ID=\''.$_REQUEST[id].'\'');
		unset($_REQUEST['modfunc']);
	}
	}
}
        else
        {
            UnableDeletePrompt('Cannot delete because it is not deletable.');
        }
}

if($_REQUEST['modfunc']!='remove')
{
	$sql = 'SELECT ID,TITLE,SHORT_NAME,TYPE FROM student_enrollment_codes WHERE SYEAR=\''.UserSyear().'\'  ORDER BY TITLE';
	$QI = DBQuery($sql);
	$codes_RET = DBGet($QI,array('TITLE'=>'makeTextInput','SHORT_NAME'=>'makeTextInput','TYPE'=>'makeSelectInput'));
	
	$columns = array('TITLE'=>'Title','SHORT_NAME'=>'Short Name','TYPE'=>'Type');
	$link['add']['html'] = array('TITLE'=>makeTextInput('','TITLE'),'SHORT_NAME'=>makeTextInput('','SHORT_NAME'),'TYPE'=>makeSelectInput('','TYPE'));
	$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove";
//	$link['remove']['link'] = "#"." onclick='check_content(\"ajax.php?modname=$_REQUEST[modname]&modfunc=remove\");'";
	$link['remove']['variables'] = array('id'=>'ID');
	
	echo "<FORM name=F1 id=F1 action=Modules.php?modname=$_REQUEST[modname]&modfunc=update method=POST>";
	#DrawHeader('',SubmitButton('Save'));
	ListOutput($codes_RET,$columns,'Enrollment Code','Enrollment Codes',$link);
	#echo '<br /><CENTER>'.SubmitButton('Save','','class=btn_medium onclick="formcheck_student_enrollment_code_F1();"').'</CENTER>';
	echo '<br /><CENTER>'.SubmitButton('Save','','class=btn_medium').'</CENTER>';
	echo '</FORM>';
}

function makeTextInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';
	
	if($name=='SHORT_NAME')
		$extra = 'size=5 maxlength=10 class=cell_floating';
		else 
		$extra = 'class=cell_floating';
	
	return TextInput($value,'values['.$id.']['.$name.']','',$extra);
}

function makeSelectInput($value,$name)
{	global $THIS_RET;
	
	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';
	
	if($name=='TYPE')
		$options = array('Add'=>'Add','Drop'=>'Drop','Roll'=>'Roll Over','TrnD'=>'Drop(Transfer)','TrnE'=>'Enroll(Transfer)');
	
	return SelectInput($value,'values['.$id.']['.$name.']','',$options);
}

function makeCheckBoxInput($value,$name)
{	global $THIS_RET;

	if($THIS_RET['ID'])
		$id = $THIS_RET['ID'];
	else
		$id = 'new';

	return CheckBoxInput($value,'values['.$id.']['.$name.']');
}



?>
